﻿#ifndef PST_INFOMATIONCURVE2DBASICAL_H
#define PST_INFOMATIONCURVE2DBASICAL_H

#include <QObject>
#include <QList>
#include <QPair>
#include <QSet>
#include <QVector>
#include <qcustomplot.h>

namespace
{
    bool areVectorsEqual(const QSet<double>& vec1, const QSet<double>& vec2)
    {
        // 首先检查大小  
        if (vec1.size() != vec2.size()) {
            return false; // 大小不相等直接返回 false  
        }

        // 使用 std::equal 比较内容  
        return std::equal(vec1.begin(), vec1.end(), vec2.begin());
    }

    // 查找多个 QVector 的公共字符串
    QList<QString> findCommonStrings(const QList<QVector<QString>>& vectors)
    {
        // 如果没有QVector，则返回空列表  
        if (vectors.isEmpty()) {
            return QList<QString>();
        }

        // 使用第一个QVector作为基准集  
        QSet<QString> commonStrings = QSet<QString>::fromList(QList<QString>::fromVector(vectors[0]));

        // 迭代其他QVector  
        for (int i = 1; i < vectors.size(); ++i) {
            QSet<QString> currentSet = QSet<QString>::fromList(QList<QString>::fromVector(vectors[i]));
            commonStrings.intersect(currentSet); // 找到交集  
        }

        // 将结果转换为QList并返回  
        return commonStrings.toList();
    }
}

namespace pst
{
    class Graph2D;
    class TextFileParser;

    class InfomationCurve2DBasical :public QObject
    {
        Q_OBJECT
    public:
        InfomationCurve2DBasical(QObject* parent = nullptr)
            : QObject(parent)
        {
        }

        static bool getCommonData(const QList<InfomationCurve2DBasical>& dataList, InfomationCurve2DBasical& commonInfo);


        InfomationCurve2DBasical operator=(const InfomationCurve2DBasical& other)
        {
            this->m_isMuti = other.m_isMuti;
            this->m_x = other.m_x;
            this->m_y = other.m_y;
            this->m_addedGraph = other.m_addedGraph;
            this->m_reader = other.m_reader;
            this->m_scanParaIndex = other.m_scanParaIndex;
            this->m_curveLineAddress = other.m_curveLineAddress;
            this->m_otherKeyData = other.m_otherKeyData;
            this->m_valueIndex = other.m_valueIndex;
            this->m_isNormalized = other.m_isNormalized;
            this->m_isDB = other.m_isDB;
            this->m_scanParaName = other.m_scanParaName;
            return *this;
        }

        InfomationCurve2DBasical(const InfomationCurve2DBasical& other)
        {
            this->m_isMuti = other.m_isMuti;
            this->m_x = other.m_x;
            this->m_y = other.m_y;
            this->m_addedGraph = other.m_addedGraph;
            this->m_reader = other.m_reader;
            this->m_scanParaIndex = other.m_scanParaIndex;
            this->m_curveLineAddress = other.m_curveLineAddress;
            this->m_otherKeyData = other.m_otherKeyData;
            this->m_valueIndex = other.m_valueIndex;
            this->m_isNormalized = other.m_isNormalized;
            this->m_isDB = other.m_isDB;
            this->m_scanParaName = other.m_scanParaName;
        }

        virtual ~InfomationCurve2DBasical() {};

        //是否有公共的扫描参数, 单个文件默认有，
        //当本类当作多个类的交集时，用于判断是否有公共的扫描参数
        //void setHasCommonScanParameter(bool hasCommon) { m_hasCommonScanParameter = hasCommon; }
        //bool getHasCommonScanParameter() const { return m_hasCommonScanParameter; }

        //void setIsAllOtherKeySame(bool isSame) { m_isAllOtherKeySame = isSame; }
        //bool getIsAllOtherKeySame() const { return m_isAllOtherKeySame; }

        //void setValueIndexSame(bool isSame) { m_isValueIndexSame = isSame; }
        //bool getValueIndexSame() const { return m_isValueIndexSame; }

        //void setIsAllNormalized(int isNormalized) { m_isAllNormalized = isNormalized; }
        //int getIsAllNormalized() const { return m_isAllNormalized; }

        //void setIsAllDB(int isDB) { m_isAllDB = isDB; }
        //int getIsAllDB() const { return m_isAllDB; }
        inline void setIsMuti(bool isMuti) { m_isMuti = isMuti; }
        inline bool getIsMuti() const { return m_isMuti; }

        //传入标题名称和数据
        inline void setXData(const QPair<QString, QVector<double>>& x) { m_x = x; }
        inline QPair<QString, QVector<double>> getXData() const { return m_x; }

        //传入标题名称和数据
        inline void setYData(const QPair<QString, QVector<double>>& y) { m_y = y; }
        inline QPair<QString, QVector<double>> getYData() const { return m_y; }
        inline QPair<QString, QVector<double>>* getYDataPointer() { return &m_y; }

        inline void setCurveLineAddress(QCPGraph* address) { m_curveLineAddress = address; }
        inline QCPGraph* getCurveLineAddress() const { return m_curveLineAddress; }

        inline void setGraph(Graph2D* graph) { m_addedGraph = graph; }
        inline Graph2D* getGraph() const { return m_addedGraph; }

        inline void setReader(TextFileParser* reader) { m_reader = reader; }
        inline TextFileParser* getReader()const { return m_reader; }

        inline void setScanParameterName(const QString& name) { m_scanParaName = name; }
        inline QString getScanParameterName() const { return m_scanParaName; }

        inline void setScanParameterIndex(int index) { m_scanParaIndex = index; }
        inline int getScanParameterIndex() const { return m_scanParaIndex; }

        /// @brief 获取公共的可用作固定轴显示的列名称 只在多个曲线时使用
        inline void setCommonFixedNames(const QList<QString>& names) { m_commonFixedNames = names; }
        inline QList<QString> getCommonFixedNames() const { return m_commonFixedNames; }

        inline void setOtherKeyData(const QVector<double>& data) { m_otherKeyData = data; }
        inline QVector<double> getOtherKeyData() const { return m_otherKeyData; }
        inline QVector<double>* getOtherKeyDataPoinyer() { return &m_otherKeyData; }

        inline void setValueIndex(int index) { m_valueIndex = index; }
        inline int getValueIndex() const { return m_valueIndex; }

        inline void setIsNormalized(int isNormalized) { m_isNormalized = isNormalized; }
        inline int getIsNormalized() const { return m_isNormalized; }

        inline void setIsDB(int isDB) { m_isDB = isDB; }
        inline int getIsDB() const { return m_isDB; }

    protected:
        //当本类当作多个类的交集时，用于判断并存储信息
        //是否有公共的扫描参数, 单个文件默认有，
        //bool m_hasCommonScanParameter{ true };
        //bool m_isAllOtherKeySame{ true };
        //bool m_isValueIndexSame{ true };
        ////是否取归一化，- 1：未定义；0，没有取；其他：取了归一化
        //int m_isAllNormalized{ 0 };
        ////是否取对数，-1：未定义；0，没有取；其他：取了归一化
        //int m_isAllDB{ 0 };
        bool m_isMuti{ false };// 是否是多个曲线
        //原始数据
        QPair<QString, QVector<double>> m_x{}; //存的是原始数据
        QPair<QString, QVector<double>> m_y{};
        QString m_scanParaName{};//扫描参数名字，不用下标（相同的名字在不同文件中可能是不同的下标）
        Graph2D* m_addedGraph{ nullptr };//曲线添加到了哪个图上
        QCPGraph* m_curveLineAddress{ nullptr };
        TextFileParser* m_reader{ nullptr }; //曲线由哪个reader产生
        int m_scanParaIndex{ -1 }; //扫描参数下标
        QList<QString> m_commonFixedNames{};//共同的可用作变量轴的列名
        QVector<double> m_otherKeyData{}; //其他key对应的数值，每个曲线对于每个key只对应一个值
        int m_valueIndex{ -1 }; //对应的value下标
        int m_isNormalized{ 0 }; // -1：未定义,部分选中；0，没有取；其他：取了归一化
        int m_isDB{ 0 };
        //-1：未定义；0，没有取；其他：取了
    };
}
#endif
